package com.dao.impl;

import com.dao.AdminInfoDAO;
import com.entity.AdminInfo;
import util.DBUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminInfoDAOImpl implements AdminInfoDAO {
    @Override
    public int insert(AdminInfo entity) throws SQLException {
        String sql = "INSERT INTO admin_info(admin_id,admin_name,admin_pwd,admin_create_time) values(?,?,?,?)";
        try(
                Connection conn = DBUtil.getConnection();
                PreparedStatement pstmt = conn.prepareStatement(sql);
        ){
            pstmt.setObject(1, entity.getAdminId());
            pstmt.setObject(2,entity.getAdminName());
            pstmt.setObject(3,entity.getAdminPwd());
            pstmt.setObject(4,entity.getAdminCreateTime());
            return pstmt.executeUpdate();
        }
    }

    @Override
    public int updateById(AdminInfo entity) throws SQLException {
        String sql = "update admin_info set admin_name=?,admin_pwd=?,admin_create_time=? where admin_id=?";
        try(
                Connection conn = DBUtil.getConnection();
                PreparedStatement pstmt = conn.prepareStatement(sql);
        ){
            pstmt.setObject(4, entity.getAdminId());
            pstmt.setObject(1,entity.getAdminName());
            pstmt.setObject(2,entity.getAdminPwd());
            pstmt.setObject(3,entity.getAdminCreateTime());
            return pstmt.executeUpdate();
        }
    }

    @Override
    public int deleteById(Serializable id) throws SQLException {
        String sql = "delete from admin_info  where admin_id=?";
        try(
                Connection conn = DBUtil.getConnection();
                PreparedStatement pstmt = conn.prepareStatement(sql);
        ){
            pstmt.setObject(1, id);
            return pstmt.executeUpdate();
        }
    }

    @Override
    public AdminInfo selectById(Serializable id) throws SQLException {
        String sql = "select * from admin_info  where admin_id=?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();
            if(rs.next()){
                AdminInfo entity = new AdminInfo();
                entity.setAdminId(rs.getInt("admin_id"));
                //如果该字段可能为空
                entity.setAdminName(rs.getString("admin_name"));
                entity.setAdminPwd(rs.getString("admin_pwd"));
                entity.setAdminCreateTime(rs.getDate("admin_create_time"));
                return entity;
            }else{
                throw new SQLException("没有找到对应的记录");
            }
        }finally {
            DBUtil.close(conn,pstmt,rs);
        }
    }

    @Override
    public List<AdminInfo> select(int index, int len) throws SQLException {
        String sql = "select * from admin_info  order by admin_id limit ?,?";//只要是返回多条记录,一定记得+order by子句
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, index);
            pstmt.setObject(2, len);
            rs = pstmt.executeQuery();
            List<AdminInfo> list = new ArrayList<>();
            while (rs.next()) {
                AdminInfo entity = new AdminInfo();
                entity.setAdminId(rs.getInt("admin_id"));
                //如果该字段可能为空
                entity.setAdminName(rs.getString("admin_name"));
                entity.setAdminPwd(rs.getString("admin_pwd"));
                entity.setAdminCreateTime(rs.getDate("admin_create_time"));
                list.add(entity);
            }
            return list;
        }finally {
            DBUtil.close(conn,pstmt,rs);
        }
    }

    @Override
    public long count() throws SQLException {
        String sql = "select count(*) from admin_info";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()){
                return rs.getLong(1);
            }else{
                throw new SQLException("没有找到对应的记录");
            }
        }finally {
            DBUtil.close(conn,pstmt,rs);
        }
    }
}
